package com.lunatic.cli.controller;

import com.lunatic.cli.entity.DemoEntity;
import com.lunatic.cli.service.BaseService;
import com.lunatic.cli.service.impl.BaseServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;
import java.util.Random;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author little_lunatic
 * @since 2025-06-04
 */
@Api("demo control")
@Controller
@RequestMapping("/demo")
public class DemoController {

    @Resource
    private BaseServiceImpl baseService;

    @ApiOperation("增")
    @PostMapping
    public ResponseEntity<Boolean> add(){
        DemoEntity demoEntity = new DemoEntity();
        demoEntity.setName("name" + new Random().nextInt(1000));
        boolean save = baseService.save(demoEntity);
        return ResponseEntity.ok(save);
    }

    @ApiOperation("删")
    @DeleteMapping
    public ResponseEntity<Boolean> del(){
        DemoEntity demoEntity = new DemoEntity();
        demoEntity.setName("update" + new Random().nextInt(1000));
        demoEntity.setId(new Random().nextInt(100));
        boolean del = baseService.removeById(demoEntity);
        return ResponseEntity.ok(del);
    }

    @ApiOperation("改")
    @PutMapping
    public ResponseEntity<Boolean> update(){
        DemoEntity demoEntity = new DemoEntity();
        demoEntity.setName("update" + new Random().nextInt(1000));
        boolean update = baseService.updateById(demoEntity);
        return ResponseEntity.ok(update);
    }

    @ApiOperation("查")
    @GetMapping
    public ResponseEntity<List> get(){
        List<DemoEntity> list = baseService.list();
        return ResponseEntity.ok(list);
    }
}
